﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Export;
using WmsApp.Service;
using WmsApp.StaticData;
using WmsApp.StaticData.Unity;
using WmsApp.Style;
using WmsApp.UI;
using WmsApp.WindowControl;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormInInvoice : BaseForm
    {
        private BaseGoodsCodeTypeObserver goodsCodeTypeObserver = null;
        private InStoreBillController inStoreBillController = null;
        public FormInInvoice()
        {
            InitializeComponent();

            //this.qInStoreBill1.AddCustomerCondition(" and BillState = 1 ");
            //this.qInStoreBillDtl1.AddCustomerCondition("  and BillID in ( select BillID from dbo.InStoreBill  where  BillState = 1 ) ");

            if (GlobalClass.RunTimeMode)
            {
                inStoreBillController = new InStoreBillController(this.textEditBillID3, this.lookUpEditProvider3,
                                                                  this.textEditProviderName3,
                                                                  this.dateEditBillDate3, this.textEditInBillStateDesc3,
                                                                  this.gridControlEdit3, this.lookUpEditInStoreType3,
                                                                  //this.gridControlInvoiceBill,
                                                                  null,
                                                                  this.textEditRequisitionNo3,
                                                                  this.textEditPurchaseName3,
                                                                  this.textEditProjectNo3,
                                                                  this.textEditKeeperName3,
                                                                  this.textEditContractNo3,
                                                                  this.textEditCheckerName3,
                                                                  this.memoEditRemark3,
                                                                  this.lookUpEditPurchaseID3,
                                                                  this.lookUpEditKeeperID3,
                                                                  this.lookUpEditCheckerID3,
                                                                  this.comboBoxEditPurchaseID3,
                                                                  this.comboBoxEditKeeperID3,
                                                                  this.comboBoxEditCheckerID3,
                                                                  this.dateEditInBillDate3);
                inStoreBillController.TextEditStateConfirmInvoiceFlag = this.textEditStateConfirmInvoiceFlag;
                inStoreBillController.TextEditConfirmInvoiceName = this.textEditConfirmInvoiceName;
                inStoreBillController.TextEditConfirmInvoiceID = this.textEditConfirmInvoiceID;
                inStoreBillController.DateEditConfirmInvoiceDate = this.dateEditConfirmInvoiceDate;
                try
                {
                    var subject = StaticData.GoodsCodeTypeSubjectFactory.GetGoodsCodeTypeSubject();
                    goodsCodeTypeObserver = new GoodsCodeTypeObserver("in", subject,
                                                                      inStoreBillController.FRepositoryItemGridLookUpEdit, this);
                    subject.Attach(goodsCodeTypeObserver);

                    //重构后的代码规范，就一句话
                    SubjectFactory.AddObserverToSubject(new CustomerObserver("customer", SubjectFactory.GetSubject(), this.lookUpEditProvider3.Properties, this));
                }
                catch (Exception ex)
                {
                    Exceptions.ExceptionHandler.ShowException(ex);
                }
            }
        }

        public override string HeadText
        {
            get
            {
                return "发票入库";
            }
        }

        private void CmdEdit2_Click(object sender, EventArgs e)
        {
            var obj = this.gridView2.GetFocusedRow();
            if (obj != null)
            {
                this.inStoreBillController.LoadInStoreBill(((InStoreBillDtl)obj).BillID);
                this.xtraTabControlMain.SelectedTabPage = this.xtraTabPage3;
            }
        }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            IEntityService<InStoreBill, InStoreBillService> service =
        EntityServiceFactory.GetEntityService<InStoreBill, InStoreBillService>();
            IQueryControl qControl = this.qInStoreBill1;
            this.gridControlEdit1.DataSource = service.GetEntityList(qControl.GetCondition());
            RowStyleConditionController.AddStyleFormatCondition(this.gridView1, "BillStateDesc", "[BillStateDesc] != '确认'");
        }

        private void CmdEdit1_Click(object sender, EventArgs e)
        {
            var obj = this.gridView1.GetFocusedRow();
            if (obj != null)
            {
                this.inStoreBillController.LoadInStoreBill(((InStoreBill)obj).BillID);
                this.xtraTabControlMain.SelectedTabPage = this.xtraTabPage3;
            }
        }

        private void CmdQuery2_Click(object sender, EventArgs e)
        {
            IEntityService<InStoreBillDtl, InStoreBillDtlService> service =
               EntityServiceFactory.GetEntityService<InStoreBillDtl, InStoreBillDtlService>();
            IQueryControl queryControl = this.qInStoreBillDtl1;
            this.gridControlEdit2.DataSource = service.GetEntityList(queryControl.GetCondition());
        }

        private void RepositoryItemButtonEditInvoiceButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            try
            {
                var inStoreBillDtl = (InStoreBillDtl)this.gridView3.GetRow(this.gridView3.FocusedRowHandle);
                var form = new FormInvoiceBillInStoreBillDtlSelector(inStoreBillDtl.BillDtlID,inStoreBillController.CurrentBillProviderCode);
                form.ShowDialog();
                if(form.FInStoreBillDtl != null)
                {
                    inStoreBillDtl.InvoiceNoList = form.FInStoreBillDtl.InvoiceNoList;
                    inStoreBillDtl.InvoiceDtlTotalAmountTax = form.FInStoreBillDtl.InvoiceDtlTotalAmountTax;
                    this.gridControlEdit3.RefreshDataSource();
                }
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdInAffirmInvoice_Click(object sender, EventArgs e)
        {
            this.inStoreBillController.AffirmInStoreBillInvoice();
        }

        private void CmdPrint_Click(object sender, EventArgs e)
        {
            this.inStoreBillController.Print();
        }

        private void CmdRefresh_Click(object sender, EventArgs e)
        {
            if (System.Windows.Forms.DialogResult.Yes == XtraMessageBox.Show("确实要重新加载吗？", "提问", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                this.inStoreBillController.RefreshInStoreBill();
            }
        }
    }
}